// -----------------------------------------------
// Author:     code34 nicolas_boiteux@yahoo.fr
// Edited by:  Neumatic
// Warcontex:  Sentinelle in static weapon
// -----------------------------------------------

private [
	"_unit",
	"_enemys",
	"_count",
	"_counter",
	"_position",
	"_gridofposition",
	"_distancemax",
	"_cible",
	"_enemyside",
	"_initialposition"
];

_unit      = _this select 0;
_enemyside = _this select 1;

if (format ["%1", _unit] == "any") exitWith {};

_enemys = [];
_count = 0;
_counter = 0;
_position = position _unit;
_gridofposition = [_position, 100, 360, getDir _unit, 10] call WC_fnc_createcircleposition;

_initialposition = getPosATL _unit;

while {alive _unit} do {
	_enemys = (getPosATL _unit) nearEntities ["CAManBase", 200];
	_distancemax = 200;
	_counter = _counter + 1;
	{
		if ((random 1 > 0.97) or (wcalert > 99)) then {
			_unit reveal _x;
		};
		if (((side _x) in _enemyside) and (_unit knowsAbout _x > 0.5)) then {
			if (_unit distance _x < _distancemax) then {_distancemax = _unit distance _x; _cible = _x};
			_count = _count + 1;
		};
		sleep 0.05;
	} foreach _enemys;

	if (_count == 0) then {
		_unit setBehaviour "SAFE";
		_unit setCombatMode "WHITE";
		_unit setSpeedMode "LIMITED";
		if ((getPosATL _unit) distance _initialposition < 100) then {
			if (vehicle _unit == _unit) then {
				if (_counter > 6) then {
					_position = [((getPosATL _unit) select 0) + ([5,20] call WC_fnc_seed), ((getPosATL _unit) select 1) + ([5,20] call WC_fnc_seed)];
					_unit doMove _position;
					_counter = 0;
				} else {
					_unit doWatch (_gridofposition call BIS_fnc_selectRandom);
				};
			} else {
				_unit doWatch (_gridofposition call BIS_fnc_selectRandom);
			};
		} else {
			_unit doMove _initialposition;
		};
	} else {
		_unit doWatch _cible;
		_unit doFire _cible;
		_unit doTarget _cible;
		if (vehicle _unit == _unit) then {
			if !(_cible distance (_unit getHideFrom _cible) > 10) then {
				_unit setBehaviour "STEALTH";
				_unit setSpeedMode "LIMITED";
			} else {
				_unit setBehaviour "COMBAT";
				_unit setCombatMode "RED";
				_unit setSpeedMode "FULL";
				_unit doMove position _cible;
			};
		} else {
			_unit setBehaviour "COMBAT";
			_unit setCombatMode "RED";
			_unit setSpeedMode "FULL";
		};

		{
			_x reveal _cible;
		} foreach units (group _unit);
	};

	_count = 0;
	sleep 5;
};